package bean;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

import javax.security.auth.Subject;
import javax.security.auth.callback.CallbackHandler;
import javax.security.auth.login.FailedLoginException;
import javax.security.auth.login.LoginException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import weblogic.security.SimpleCallbackHandler;
import weblogic.security.services.Authentication;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;

import java.io.IOException;

import javax.faces.context.ExternalContext;

import oracle.adf.share.ADFContext;
import util.ADFUtil;
import weblogic.security.URLCallbackHandler;
import java.util.Map;
import java.util.HashMap;

import oracle.adf.controller.ControllerContext;
import oracle.adf.controller.TaskFlowId;

public class Login {
    private String _username;
    private String _password;
    
    public void setUsername(String _username) {
        this._username = _username.toLowerCase();
    }

    public String getUsername() {
        return _username;
    }

    public void setPassword(String _password) {
        this._password = _password;
    }

    public String getPassword() {
        return _password;
    }
    
    public String doLogin() {
        String un = _username;
        byte[] pw = _password.getBytes();
        FacesContext ctx = FacesContext.getCurrentInstance();
        HttpServletRequest request = (HttpServletRequest)ctx.getExternalContext().getRequest();
        try {
            Subject subject = Authentication.login(new URLCallbackHandler(un, pw));
            weblogic.servlet.security.ServletAuthentication.runAs(subject, request);
          
//           // String loginUrl = "/adfAuthentication?success_url=/faces/main";
//            HttpServletResponse response = (HttpServletResponse)ctx.getExternalContext().getResponse();
//           
//          
//            sendForward(request, response, loginUrl);
            
             FacesContext fctx = FacesContext.getCurrentInstance();
             ExternalContext ectx = fctx.getExternalContext();
            try
               {
               ectx.redirect( this.getTaskFlowTestURL() );
               } catch ( IOException e )
               {
               e.printStackTrace();
               }
        } catch (FailedLoginException fle) {
            FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Incorrect Username or Password", "An incorrect Username or Password was specified");
            ctx.addMessage(null, msg);
        } catch (LoginException le) {
            reportUnexpectedLoginError("LoginException", le);
        }
        return null;
    }

    private void sendForward(HttpServletRequest request, HttpServletResponse response, String forwardUrl) {
        FacesContext ctx = FacesContext.getCurrentInstance();
        RequestDispatcher dispatcher = request.getRequestDispatcher(forwardUrl);
        try {
            dispatcher.forward(request, response);
        } catch (ServletException se) {
            reportUnexpectedLoginError("ServletException", se);
        } catch (IOException ie) {
            reportUnexpectedLoginError("IOException", ie);
        }
        ctx.responseComplete();
    }

    private void reportUnexpectedLoginError(String errType, Exception e) {
        FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Unexpected error during login",
                             "Unexpected error during login (" + errType + "), please consult logs for detail");
        FacesContext.getCurrentInstance().addMessage(null, msg);
        e.printStackTrace();
    }
    
    private String getTaskFlowTestURL()
    {
         String tfId = "MainFlow";
         String tfDoc = "/WEB-INF/flow/MainFlow.xml";
         Map<String, Object> params = new HashMap<String, Object>();
         params.put( "userName", _username );

         TaskFlowId taskFlowId = new TaskFlowId( tfDoc, tfId );
         String url = ControllerContext.getInstance().getTaskFlowURL( false, taskFlowId, params );
         return url;
    }

}

